/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.ui;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.ui.actionlisteners.AddTableAction;
import project.jpdftools.ui.actionlisteners.AddTablePopUpMenuButtonAction;

/**
 *
 * @author Mido
 */
public class AddTablePopUpMenu {

    private JPopupMenu menu = new JPopupMenu();
    private static AddTablePopUpMenu add_table_popup_menu;

    private AddTablePopUpMenu() {
        int rows = 10;
        int columns = 10;
        // array to hold all the buttons
        JButton[][] table_dimension = new JButton[rows][columns];
        // panel to organize the buttons inside
        JPanel panel = new JPanel(new GridLayout(rows, columns));
        // label to show the selected  buttons inside
        JLabel table_size_label = new JLabel(LocalizationManager.getCurrent_resource().getString("ADD_TABLE_PANEL_TITLE"));
        // intialize the array of buttons
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                // intialize the button object
                table_dimension[i][j] = new JButton();
                // add action listener
                table_dimension[i][j].addActionListener(new AddTablePopUpMenuButtonAction(table_dimension, menu, table_size_label, i, j));
                // add mouse listener
                table_dimension[i][j].addMouseListener(new AddTablePopUpMenuButtonAction(table_dimension, menu, table_size_label, i, j));
                // set button size
                table_dimension[i][j].setPreferredSize(new Dimension(20, 20));
                // add the button to the panel
                panel.add(table_dimension[i][j]);
            }
        }
        // add a label to the menu
        menu.add(table_size_label);
        // add the panel to the popup menu
        menu.add(panel);
        // add a separator
        menu.add(new JSeparator());
        // menu item to open the addTablePanel
        JMenuItem add_tabel_panel = new JMenuItem(LocalizationManager.getCurrent_resource().getString("ADD_TABLE_MENU_ITEM"));
        // add action listener to the menu item
        add_tabel_panel.addActionListener(new AddTableAction());
        // add the meni item to the popup menu
        menu.add(add_tabel_panel);
    }

    /**
     * Get the popup menu
     *
     * @return
     */
    public JPopupMenu getMenu() {
        return menu;
    }

    /**
     * Returns the AddTablePopUpMenu instance
     *
     * @return
     */
    public static AddTablePopUpMenu getInstance() {
        if (add_table_popup_menu == null) {
            add_table_popup_menu = new AddTablePopUpMenu();
        }
        return add_table_popup_menu;
    }
}
